
using UnityEngine;
using UnityEditor;

using System;
using Unity.VisualScripting;

namespace lights.polytool.editor
{
    [CustomEditor(typeof(comp_quickmesh))]
    public class inspector_quickmesh : Editor
    {

        public override void OnInspectorGUI()
        {

            base.OnInspectorGUI();
            var _target = this.target as comp_quickmesh;
            if (_target == null)
            {
                return;
            }

            GUITool.ColorLabel("QuickMesh.", 24, Color.red);
            EditorGUILayout.BeginHorizontal();

            for (var i = 0; i < 4; i++)
            {
                var c1 = EditorGUILayout.ColorField(_target._colors[i]);
                if (c1 != _target._colors[i])
                {
                    _target._colors[i] = c1;
                    _target.UpdateAllChildColor();
                }
            }


            EditorGUILayout.EndHorizontal();

            CreateGUI(_target.transform);

            GUILayout.Space(8);
            _target.output_obj = EditorGUILayout.ObjectField("Output", _target.output_obj, typeof(GameObject)) as GameObject;
            var rpath = AssetDatabase.GetAssetPath(_target.output_obj);
            GUILayout.Label("respath=" + rpath);
            if (GUITool.ColorButton("Bake", 24, new Color(0.8f, 0.5f, 0.3f, 1.0f)))
            {
                if (!AssetDatabase.IsValidFolder("Assets/resources"))
                    AssetDatabase.CreateFolder("Assets", "resources");
                if (!AssetDatabase.IsValidFolder("Assets/resources/quickmesh"))
                    AssetDatabase.CreateFolder("Assets/resources", "quickmesh");
                if (_target.output_obj == null)
                {
                    if (EditorUtility.DisplayDialog("warning", "no give a output object. will write to resources/quickmesh/", "ok", "cancel"))
                    {



                        var time = DateTime.Now;
                        var id = time.Year.ToString("d04") + time.Month.ToString("d02") + time.Day.ToString("d02")
                           + "_" + time.Hour.ToString("d02") + time.Minute.ToString("d02") + time.Second.ToString("d02");

                     

                       var obj= _target.output_obj = new GameObject();

                        _target.output_obj.name = _target.gameObject.name + "_bake";
                        _target.Bake();
                        var prefabname = "assets/resources/quickmesh/bake_" + id + ".prefab";
                        PrefabUtility.SaveAsPrefabAsset(obj, prefabname);

                      
                        _target.output_obj = AssetDatabase.LoadAssetAtPath(prefabname, typeof(GameObject)) as GameObject;

                        GameObject.DestroyImmediate(obj);
                    }
                }
                else
                {
                    _target.Bake();
                    if (string.IsNullOrEmpty(rpath) == false)
                    {
                        EditorUtility.SetDirty(_target.output_obj);

                        AssetDatabase.SaveAssetIfDirty(_target.output_obj);
                        PrefabUtility.SavePrefabAsset(_target.output_obj);
                    }
                }
            }

        }
        public static void CreateGUI(Transform tran)
        {
            GUILayout.Space(8);
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Create SubMesh"))
            {
                var obj = new GameObject("_polymesh");
                obj.transform.parent = tran;
                obj.AddComponent<MeshFilter>();
                obj.AddComponent<MeshRenderer>().material = new Material(Shader.Find("lights/polyshader_single"));
                obj.AddComponent<comp_polymesh>().InitQuad();
                var elem = obj.AddComponent<comp_quickmesh_elem>();

                elem._colorindex = UnityEngine.Random.Range(0, 4);
                elem.InitColor();

                Vector3 localpos = new Vector3(UnityEngine.Random.value * 2, UnityEngine.Random.value * 2, UnityEngine.Random.value * 2);

                obj.transform.localPosition = Vector3.Normalize(localpos);

            }

            EditorGUILayout.EndHorizontal();
        }


        void OnSceneGUI()
        {

            var _target = this.target as comp_quickmesh;
            GizmoTool.DrawGizmoRoot(_target, true);

            //GizmoTool.DrawGUIRoot(_target);

        }


    }
}
